
package JET.editor;

/**
 *
 * @author 057Ry
 */
public class DataVector {
    private static final int initialCapacity = 100;
    private static final int extendCount = 10;

    public double[] values;
    public int count = 0;

    public DataVector() {
        values = new double[initialCapacity];
    }

    public DataVector(int initSize) {
        values = new double[initSize];
    }

    public void add(double v) {
        if (count<values.length)
            values[count++] = v;
        else {
            double[] newValues = new double[values.length+extendCount];
            for(int i=0; i<values.length; i++)
                newValues[i] = values[i];
            newValues[count++] = v;

            values = newValues;
        }
    }
    
    public void set(int index, double v) {
        if (index>=0 && index<values.length) {
            values[index] = v;
            if (count<=index)
                count = index+1;
        } else if (index>=values.length) {
            int dif = index-values.length + 1;
            double[] newValues = new double[values.length + dif + extendCount];
            for(int i=0; i<count; i++)
                newValues[i] = values[i];
            
            newValues[index] = v;
            values = newValues;
            count = index+1;
        }
    }

    public void remove(int index) {
        if (count<=0)
            return;

        for(int i=index; i<values.length-1; i++)
            values[i] = values[i+1];
         count--;
    }

    public double get(int index) {
        return values[index];
    }

    public double[] getValues() {
        return values;
    }

    public int getCount() {
        return count;
    }
    
    public String toString() {
        String s = "DataVector[";
        for(int i=0; i<count; i++)
            s += values[i] + (i!=count-1? ", " : "]");
        return s;
    }

    public static void main(String[] args) {
        DataVector dv = new DataVector(1);

        for(int i=5; i<13; i++) {
            dv.set(i, i);
        }

        for(int i=100; i<105; i++) {
            dv.add(i);
        }

        dv.set(20, 20);

        for(int i=20; i>=0; i--)
            dv.remove(i);

        System.out.println(dv);
    }
}